<template>
  <div class="vote">
    <!-- <div class="title">平时考核系统民主测评表</div> -->
    <div class="title">平时考核系统民主测评表</div>
    <div class="desc">{{ detailsData.assessmentSecondTitle }}</div>
    <div class="desc">考核单位：{{ detailsData.createByOrgName }}</div>
    <div class="tips">
      <div class="tips_title">温馨提示</div>
      <div class="tips_li">● 本次民主测评采用不记名测评形式，请认真对待此次测评；</div>
      <div class="tips_li">● 随机码一人一码，仅一次有效不能重复使用。</div>
    </div>
    <div class="filed">
      <van-field v-model="randomCode" type="digit" placeholder="请输入随机码开始投票" input-align="center" />
    </div>
    <div class="commit">
      <!-- <div class="button" @click="goVote">开始测评</div> -->
      <van-button
        round
        type="primary"
        @click="goVote"
        :loading="loading"
        :disabled="loading"
        loading-text="提交中..."
        color="#1989fa">
        开始测评
      </van-button>
    </div>
  </div>
</template>

<script>
import { startAssessment, getAssessmentDetail } from '@api/api-democratic.js'

export default {
  name: 'vote',
  components: {},
  props: {},
  data() {
    return {
      randomCode: '',
      assessmentId: this.$route.query.assessmentId,
      loading: false,
      detailsData: {},
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    this.initdata()
  },
  methods: {
    /* 初始化数据 */
    initdata() {
      getAssessmentDetail({ id: this.assessmentId }).then(res => {
        let data = res.data
        this.detailsData = { ...data }
      })
    },
    goVote() {
      if (!this.randomCode) {
        /* this.$toast.fail({
          duration: 5000, // 持续展示 toast
          closeOnClick: true,
          message: "随机码不能为空",
        }); */
        this.$dialog.alert({
          message: '随机码不能为空',
          closeOnClickOverlay: true,
        })
        return false
      }
      this.loading = true
      startAssessment({
        id: this.assessmentId,
        randomCode: this.randomCode,
      })
        .then(res => {
          const data = res.data
          this.loading = false
          if (res.status == 200) {
            if (!res.message) {
              this.$emit('showVoteForm', this.randomCode)
            } else {
              // this.$toast.fail({
              //   duration: 5000, // 持续展示 toast
              //   closeOnClick: true,
              //   message: res.message,
              // });
              this.$dialog.alert({
                message: res.message,
                closeOnClickOverlay: true,
              })
            }
          }
        })
        .catch(err => {
          this.loading = false
        })
      // this.$router.push('/resultPage')
    },
  },
}
</script>

<style lang="scss" scoped>
.vote {
  text-align: center;
  padding: 0 16px;
  font-size: 12px;
  .title {
    font-size: 16px;
    color: #333333;
    margin-top: 50px;
    margin-bottom: 8px;
  }
  .desc {
    // font-size: 14px;
    margin-top: 5px;
  }
  .tips {
    background-color: #f2f2f2;
    margin: 30px 30px;
    padding: 10px;
    text-align: left;
    .tips_title {
      // font-size: 14px;
      font-weight: bold;
      text-align: center;
    }
    .tips_li {
      margin-top: 5px;
      line-height: 1.5;
    }
  }
  .filed {
    width: 60%;
    border: 1px solid #ccc;
    margin: auto;
    margin-top: 75px;
    .van-cell {
      height: 40px;
      line-height: 40px;
    }
  }
  .commit {
    margin-top: 50px;
    .van-button {
      width: 50%;
    }
    .button {
      background-color: #1989fa;
      color: #fff;
      font-size: 16px;
      height: 40px;
      border-radius: 20px;
      text-align: center;
      line-height: 40px;
      font-weight: 700;
      width: 50%;
      margin: auto;
    }
  }
}
</style>
